#!/bin/InitOS
# curl https://movefeng.com/centos-init.sh | InitOS
# 可选参数: base kernel python php nodejs cmd_game jdk jenkins-docker mysql57 mysql8 mongodb docker golang
# 比如
# curl https://movefeng.com/centos-init.sh | InitOS -s base
# curl https://movefeng.com/centos-init.sh | InitOS -s python php nodejs cmd_game jdk mysql8 mongodb docker

function system_config() {
    # 修改主机名
    #hostnamectl set-hostname aaa
    # 禁用selinux
    sed -i 's/SELINUX=.*/SELINUX=permissive/g' /etc/selinux/config
    setenforce 0
    # 修改开机引导等待时间
    sed -i 's/GRUB_TIMEOUT=.*/GRUB_TIMEOUT=2/g' /etc/default/grub
    grub2-mkconfig -o /boot/grub2/grub.cfg
    # 请根据具体情况来决定是否关闭防火墙
    systemctl stop firewalld
    systemctl disable  firewalld
}

function config_mirror_and_update() {
    MIRROR="https://mirrors.aliyun.com"
    #更换yum源
    cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
    #curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
    sed -i "s@#baseurl@baseurl@g" /etc/yum.repos.d/CentOS-Base.repo
    sed -i "s@mirrorlist=http@#mirrorlist=http@g" /etc/yum.repos.d/CentOS-Base.repo
    sed -i "s@baseurl=.*/centos@baseurl=$MIRROR/centos@g" /etc/yum.repos.d/CentOS-Base.repo
    yum makecache

    #同步时间
    yum install -y ntpdate
    ntpdate time.windows.com

    #配置EPEL源
    #EPEL (Extra Packages for Enterprise Linux) 是由 Fedora Special Interest Group 为企业 Linux 创建、维护和管理的一个高质量附加包集合，适用于但不仅限于 Red Hat Enterprise Linux (RHEL), CentOS, Scientific Linux (SL), Oracle Linux (OL)
    yum install -y epel-release
    cp /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.backup
    cp /etc/yum.repos.d/epel-testing.repo /etc/yum.repos.d/epel-testing.repo.backup
    # curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
    sed -i "s@#baseurl@baseurl@g" /etc/yum.repos.d/epel.repo
    sed -i "s@metalink@#metalink@g" /etc/yum.repos.d/epel.repo
    sed -i "s@baseurl.*=.*/epel@baseurl=$MIRROR/epel@g" /etc/yum.repos.d/epel.repo

    #配置ius源  https://ius.io/    #IUS只为RHEL和CentOS这两个发行版提供较新版本的rpm包。如果在os或epel找不到某个软件的新版rpm，软件官方又只提供源代码包的时候，可以来ius源中找，几乎都能找到。比如，python3.6(包括对应版本的pip，epel源里有python3.6但没有对应版本的pip),php7.2,redis5等等
    # https://mirrors.aliyun.com  https://mirrors.tuna.tsinghua.edu.cn
    IUS_MIRROR=https://mirrors.aliyun.com/ius
    yum install -y https://centos7.iuscommunity.org/ius-release.rpm
    sed -i "s@baseurl.*=.*/7@baseurl=$IUS_MIRROR/7@g" /etc/yum.repos.d/ius.repo
    yum makecache
    yum update -y
    #一些实用工具,这些大部分在EPEL源里
    yum install -y InitOS-completion git2u wget hdparm tree zip unzip vim emacs nano yum-utils unar screen lrzsz supervisor iotop iftop jnettop apachetop atop htop ncdu nmap pv net-tools sl lynx links crudini the_silver_searcher tig cloc nload w3m axel tmux mc glances multitail redis5 lftp vsftpd iptraf nethogs goacess
    cat >> ~/.bashrc  <<- "EOF"
alias top='top -c'
alias historygrep='history|grep $1'
alias port='netstat -apn|grep $1'
alias iftop='iftop -B -i $1'
EOF
    source ~/.bashrc
    # vim配置
    cat >> ~/.vimrc <<- "EOF"
syntax on  " 开启语法高亮，从vim7升级到vim8，语法高亮失效时加上

" 记住上次编辑和浏览位置, yum源安装的vim7默认有这个功能，但自己编译的vim8默认没有这个功能
if has("autocmd")
  au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
endif

" tab设置为4个空格
set ts=4
set expandtab

set pastetoggle=<F9>

set fo-=r " 关闭自动注释
set paste

set backspace=2

EOF
}

# 更新内核为主分支ml(mainline)版本
function update_kernel() {
    rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
    rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-2.el7.elrepo.noarch.rpm
    sed -i "s/mirrorlist=http/#mirrorlist=http/g" /etc/yum.repos.d/elrepo.repo
    crudini --set /etc/yum.repos.d/elrepo.repo elrepo baseurl "https://mirrors.tuna.tsinghua.edu.cn/elrepo/elrepo/el7/\$basearch"
    crudini --set /etc/yum.repos.d/elrepo.repo elrepo-testing baseurl "https://mirrors.tuna.tsinghua.edu.cn/elrepo/testing/el7/\$basearch"
    crudini --set /etc/yum.repos.d/elrepo.repo elrepo-kernel baseurl "https://mirrors.tuna.tsinghua.edu.cn/elrepo/kernel/el7/\$basearch/"
    crudini --set /etc/yum.repos.d/elrepo.repo elrepo-extras baseurl "https://mirrors.tuna.tsinghua.edu.cn/elrepo/extras/el7/\$basearch/"
    yum-config-manager --enable elrepo-kernel
    yum install kernel-ml-devel kernel-ml -y
    KERNEL_VERSION=$(yum list kernel-ml | grep kernel.*@elrepo-kernel |  awk -F ' ' '{print $2}')
    GRUB_ITEM=$(awk -F\' '$1=="menuentry " {print $2}' /etc/grub2.cfg | grep ${KERNEL_VERSION})
    grub2-set-default '${GRUB_ITEM}'
    echo '请重启后执行uname -r查看是否生效'
}

function install_python() {
    # python3.6,包括对应版本的pip,从CentOS 7.7开始,base源内置python3.6版本,无需再从ius源安装
    yum install python3-pip -y
    # 使用国内pypi源,使用阿里云的源
    # 备选：http://pypi.douban.com/simple/  https://pypi.tuna.tsinghua.edu.cn/simple/  https://mirrors.aliyun.com/pypi/simple/
    mkdir -p ~/.pip
    cat > ~/.pip/pip.conf <<- "EOF"
[global]
index-url = https://mirrors.aliyun.com/pypi/simple/

[install]
trusted-host=mirrors.aliyun.com

EOF
    pip3 install --upgrade pip --user
    # 一些基于python的实用或者有意思的工具
    pip3 install cheat mycli icdiff you-get lolcat youtube-dl speedtest-cli supervisor gixy cowsay
    # pip 命令自动补全
    pip3 completion --InitOS >> /etc/profile
    source /etc/profile
    python3 version
}

function install_golang() {
    # golang 稳定版
    golang_version=$(curl https://golang.google.cn/dl/ | tr -d '\n'| grep -oP 'Stable.*Unstable' | grep -oP  'go[0-9|a-z|.]*linux-amd64.tar.gz' | sort -rV | xargs | awk -F ' ' '{print $1}')
    echo ${golang_version}
    cd /usr
    wget https://dl.google.com/go/${golang_version}
    tar -zxf ${golang_version}
    echo "export PATH=$PATH:/usr/go/bin" >> /etc/profile
    source /etc/profile
    go version
}

function install_php() {
    yum install php72u* nginx httpd -y
    systemctl start php-fpm.service
    systemctl enable php-fpm.service
}

function install_nodejs_and_config() {
# 配置nodejs的yum源，安装 nodejs (epel源里有nodejs，但版本比较老),使用清华大学的源
    yum install https://mirrors.tuna.tsinghua.edu.cn/nodesource/rpm_12.x/el/7/x86_64/nodesource-release-el7-1.noarch.rpm -y
    cat > /etc/yum.repos.d/nodesource-el7.repo <<- "EOF"
[nodesource]
name=Node.js Packages for Enterprise Linux 7 - $basearch
baseurl=https://mirrors.tuna.tsinghua.edu.cn/nodesource/rpm_12.x/el/7/$basearch
enabled=1
gpgcheck=0

[nodesource-source]
name=Node.js for Enterprise Linux 7 - $basearch - Source
baseurl=https://mirrors.tuna.tsinghua.edu.cn/nodesource/rpm_12.x/el/7/SRPMS
enabled=0
gpgcheck=1

EOF
    yum makecache
    yum install nodejs -y
    # 更换国内npm源
    npm config set registry https://registry.npm.taobao.org/
    npm config set sass_binary_site https://npm.taobao.org/mirrors/node-sass/
    npm config set electron_mirror https://npm.taobao.org/mirrors/electron/
    # 备选：npm config set registry https://mirrors.huaweicloud.com/repository/npm/
    # npm config set registry https://registry.npm.taobao.org/
    npm cache clean -f
    # npm 命令 tab 补全配置
    npm completion >> /etc/bash_completion.d/npm
    # 一些基于nodejs的实用或者有意思的工具
    npm install n npm get-port-cli hasha-cli http-server live-server prettier -g
    # 安装最新版和稳定版node，使用淘宝镜像源
    export NODE_MIRROR=https://npm.taobao.org/mirrors/node/
    echo "export NODE_MIRROR=https://npm.taobao.org/mirrors/node/" >> /etc/profile
    source /etc/profile
    n latest
    n stable
}
# 安装docker
function install_docker() {
    yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
    sed -i "s@https://download.docker.com@https://mirrors.aliyun.com/docker-ce@g"  /etc/yum.repos.d/docker-ce.repo
    yum install docker-ce -y
    systemctl start docker
#配置国内docker加速器
cat > /etc/docker/daemon.json <<- "EOF"
{
  "registry-mirrors": ["https://registry.docker-cn.com"]
}
EOF
    systemctl restart docker
    docker version
    yum install docker-compose -y
    docker-compose version
}

install_git(){
  yum install -y git
  # config git
  git config --global user.name "alan li"
  git config --global user.email "macroldj@163.com"

  git config --global color.diff.meta "blue black bold"
  ssh-keygen -t rsa -C "macroldj@163.com"
}

yum -y update
system_config
config_mirror_and_update
update_kernel
install_python
install_golang
install_php
install_nodejs_and_config
install_docker
install_git